<template>
  <div class="login-container">
    <div v-if="deviceType" class="child left">
      <!-- <img src="@/assets/login/logo.png" class="logo"/> -->
      <div class="loginBg">
        <img src="@/assets/login/loginBg.png" class="loginBgImage" />
        <div class="slogan">“为老年人提供更多的便利”</div>
      </div>

    </div>
    <div class="child right">
      <img src="@/assets/login/colorBg.png" class="colorBg"/>
      <!-- 登录的表单 -->
      <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          autocomplete="on"
          label-position="left"
      >
        <div class="textWe">欢迎登录</div>
        <div class="textLog">社区老年人服务系统</div>
        <div style="margin-top:97px">
          <!-- 用户名 -->
          <el-form-item prop="username" >
            <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="请输入用户名"
                name="username"
                type="text"
                tabindex="1"
                autocomplete="on"
            >
              <img slot="prefix" src="@/assets/login/icon_user.png" class="iconUser"/>
            </el-input>
          </el-form-item>
        </div>


        <el-tooltip
            v-model="capsTooltip"
            style="margin-top:16px;"
            content="Caps lock is On"
            placement="right"
            manual
        >
          <el-form-item prop="password">
            <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="请输入密码"
                name="password"
                tabindex="2"
                autocomplete="on"
                @keyup.native="checkCapslock"
                @blur="capsTooltip = false"
                @keyup.enter.native="handleLogin"
            >
              <img slot="prefix" src="@/assets/login/icon_lock.png" class="iconUser"/>
              <div slot="suffix">
                  <span class="show-pwd" @click="showPwd">
                  <svg-icon
                      :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
                  />
              </span>
              </div>
            </el-input>
          </el-form-item>
        </el-tooltip>
        <div style="text-align: center">
          <el-button
              :loading="loading"
              type="primary"
              @click.native.prevent="handleLogin"
              class="loginBt"
          >
            登录
          </el-button>
        </div>

      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      deviceType: true,
      // 登录表单对象
      loginForm: {
        username: "",
        password: "",
      },
      // 表单验证规则
      loginRules: {
        username: [
          {required: true, trigger: "blur", message: "请输入用户名"},
        ],
        password: [{required: true, trigger: "blur", message: "请输入密码"}],
      },
      passwordType: "password",
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true,
    },
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    this.checkDeviceType();
    window.addEventListener("resize", this.checkDeviceType);
    // 如果用户名为空则交代聚焦到用户名输入框
    if (this.loginForm.username === "") {
      this.$refs.username.focus();
      // 如果用户名为空则交代聚焦到用密码输入框
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.checkDeviceType);
  },
  methods: {
    checkDeviceType() {
      // const width = window.innerWidth;
      // this.deviceType = width > 768 ? 'desktop' : 'mobile';
      const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
      console.log(isMobile);
      if (isMobile) {
        console.log("当前在手机端");
        this.deviceType = false;
      } else {
        console.log("当前在PC端");
        this.deviceType = true;
      }
    },
    // checkCapslock是输入的时候题试大小写的
    checkCapslock(e) {
      const {key} = e;
      this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    },

    /**
     * 是否显示密码
     */
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    /**
     * 处理登录请求
     */
    handleLogin() {
      //进行表单验证
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true; //显示加载的动画
          //发送登录请求（调用Vuex中的user/login）
          console.log(this.loginForm);
          this.$store
              .dispatch("user/login", this.loginForm)
              .then(() => {
                this.$router.push({
                  path: this.redirect || "/",
                  query: this.otherQuery,
                });
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
        } else {
          // console.log("error submit!!");
          return false;
        }
      });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },
  },
};
</script>


<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

/* 或者使用 !important */


.login-container {
  min-height: 100%;
  width: 100vw;
  background: #FEFEFE;
  overflow: hidden;

  .login-form {
    width: 528px;
    height: 600px;
    background: rgba(255, 255, 255, 0.4);
    border-radius: 16px;
    border: 1px solid #dedede;
    z-index: 2;
    top: 22%;
    left: 50%;
    transform: translateX(-50%);
    position: relative;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 9px;
    left: 90%;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
    z-index: 2;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}

/* 手机端样式 */
@media (max-width: 600px) {
  .login-container {
    .login-form {
      width: 90% !important;
      left: 50% !important;
      transform: translateX(-50%);
      top: 15% !important;
      .el-form-item {
        width: 300px;
      }
    }
  }


  // .el-input {
  //   width: 80% !important;
  // }
  .el-input {
    width: 300px !important;
  }
  .el-input input {
    width: 100% !important;
  }
}

.iconUser {
  height: 24px;
  width: 24px;
}

.textLog {
  height: 22px;
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  font-size: 16px;
  color: #333333;
  line-height: 22px;
  text-align: center;
  font-style: normal;
  margin-top:16px;
}

.textWe {
  height: 33px;
  font-family: PingFangSC, PingFang SC;
  font-weight: 600;
  font-size: 24px;
  color: #333333;
  line-height: 33px;
  text-align: center;
  font-style: normal;
  margin-top:72px;
}

.loginBt {
  width: 76%;
  height: 56px;
  background: 	#1E90FF;
  border-radius: 28px;
  border-color: #4169E1;
  font-weight: 600;
  font-size: 18px;
  color: #FFFFFF;
  line-height: 25px;
  text-align: center;
  margin: 48px auto 0;
}

.colorBg {
  width: 100%;
  height: 100%;
  z-index: 1;
  position: absolute;
}



.loginBg {
  top: 20%;
  left: 50%;
  transform: translateX(-50%);
  position: absolute;
  .loginBgImage {
    width: 640px;
    height: 640px;
  }
  .slogan {
    height: 25px;
    font-weight: 400;
    font-size: 18px;
    color: #666666;
    line-height: 25px;
    text-align: center;
    font-style: normal;
    position: relative;
    margin-top: 32px;
  }
}

.logo {
  width: 64px;
  height: 64px;
  top: 64px;
  left: 64px;
  position: relative;
}

.child {
  flex: 1;
  position: relative;
}

.login-container {
  display: flex;
}

/* reset element-ui css */
.login-container {
  .el-input {


    input {
      border: 0px;
      height: 56px;
      width: 400px;
      background: #f4f4f4;
      border-radius: 28px;
      // caret-color: $cursor;
      font-family: PingFangSC, PingFang SC;
      color: #999999;
      font-weight: 400;
      font-size: 14px;
      line-height: 20px;
      text-align: center;
      font-style: normal;

      &:-webkit-autofill {
        // box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #999999 !important;
      }
    }
  }

  input:-webkit-autofill,
  input:-webkit-autofill:hover,
  input:-webkit-autofill:focus,
  input:-webkit-autofill:active {
    transition: background-color 5000s ease-in-out 0s; /* 延迟背景色过渡，避免闪烁 */
    -webkit-text-fill-color: #333; /* 字体颜色 */
    background-color: #f4f4f4; /* 背景颜色 */
  }

  .el-form-item {
    border-radius: 28px;
    width: 400px;
    height: 56px;
    margin: 0 auto;
  }

  .login-form {

    ::v-deep .el-input__inner {
      height: 56px;
      border-radius: 28px;
      background: #F4F4F4;
      padding: 0 30px 0 40px;
      font-size: 14px;
      color: #999999;
    }

    ::v-deep .el-input__prefix {
      display: flex;
      align-items: center;
      margin-left: 10px;
    }

    ::v-deep .el-input__suffix {
      right: 30px;
    }
  }

}

</style>
